const http=require("http");
const fs=require("fs");
const template=require("art-template");

let user={
    id:1011,
    name:"张三",
    phone:"15898745698"
};
let users=[
    {
        id:1001,
        name:"张三",
        phone:"15898745697",
        flag:true
    },
    {
        id:1002,
        name:"张三",
        phone:"15898745695",
        flag:false
    },
    {
        id:1003,
        name:"张三",
        phone:"15898745694",
        flag:false
    },
    {
        id:1004,
        name:"张三",
        phone:"15898745693",
        flag:true
    },
    {
        id:1005,
        name:"张三",
        phone:"15898745692",
        flag:true
    },
    {
        id:1006,
        name:"张三",
        phone:"15898745691",
        flag:true
    }

];
const server=http.createServer(function(request,response){
    let path=request.url;
    if(path=="/"){
        let temp=fs.readFileSync("./template/user.html").toString();
        temp=temp.replace("{{id}}",user.id);
        temp=temp.replace("{{name}}",user.name);
        temp=temp.replace("{{phone}}",user.phone);
        response.end(temp);
    }
    if(path=="/list"){
        let listTemp="";
        for(let u of users){
            listTemp+=`
                <tr>
                    <td>${u.id}</td>
                    <td>${u.name}</td>
                    <td>${u.phone}</td>
                    <td>${u.flag?"生病":"健康"}</td>
                </tr>

            `;
        }
      
        let temp=fs.readFileSync("./template/list.html").toString();
        temp=temp.replace("{{list}}",listTemp);
       
        response.end(temp);
    }

    if(path=="/list2"){
        let temp=fs.readFileSync("./template/list2.html").toString();
        let strs=template.render(temp,{"users":users});
        response.end(strs);
    }
});

server.listen(3000);